<?php defined('SYSPATH') or die('No direct script access.');

class Model_ReviewParser_Otzovik extends Model_ReviewParser_Abstract
{
	protected static $_instance = null;

    protected $_otzovik_url = 'http://www.otzovik.com';                              // Урл Отзовика =)
	protected $_reviews_url    = null;
    
    public function parse(Model_Shop $shop)
    {
        $url = $shop->otzovik_url;
		$this->_reviews_url = trim($url);
		$n = 1;
        while ($page_data = $this->_get_page_data()) {
            //usleep(700000);
			sleep(5);
            while ($review_data = $this->_get_review_data($page_data)) {
                $id              = $this->_get_id($review_data);
				$user_link       = $this->_get_user_link($review_data);
                $user_nick       = $this->_get_user_nick($review_data);
                $date            = $this->_get_date($review_data);
                $rating          = $this->_get_rating($review_data);
				$link            = $this->_otzovik_url.'/review_'.$id.'.html';
                
                $pluses          = $this->_get_pluses($review_data);
                $minuses         = $this->_get_minuses($review_data);
                $text            = $this->_get_text($review_data);
                $u               = $this->_get_usefulness($review_data);
                
                ORM::factory('shop_review_otzovik')->add_review($shop, $id, $text, $pluses, $minuses, $rating,
                    $user_nick, $user_link, $link, $date, $u['yes'], $u['no'], $n);
                
                /*echo 'id - '.$id.'<br />';
                echo 'user_nick - '.$user_nick.'<br />';
                echo 'user_link - '.$user_link.'<br />'; 
                echo 'date - '.date('d-M-y', $date).'<br />'; 
                echo 'rating - '.$rating.'<br />'; 
                echo 'link - '.$link.'<br />'; 
                echo 'pluses - '.$pluses.'<br />'; 
                echo 'minuses - '.$minuses.'<br />'; 
                echo 'text - '.$text.'<br />'; 
                echo 'usefulness - <br />';
                print_r($u); 
                echo '<hr />';*/				
				
				$n++;
            }  
        }  
		return $n-1;
    }
    
    /*public function get_reviews_urls($model)
    {
        $urls = array();
        $model_name = trim(str_replace(array('2Gb', '4Gb', '8Gb', '16Gb', '32Gb'), '', $model->name));
        $data = $this->_get_source_code($this->search_url . urlencode($model_name));
        $data = $this->_cut_str('<table class=catproduct>', '</table>', $data); 
        while ($product = $this->_cut_str('<tr>', '</tr>', $data)) {
            $url  = $this->otzovik_url . $this->_cut_str('<a href=', '>', $product);            
            $name = strip_tags($this->_cut_str('<td class=nprods>', '</td>', $product));
            if (strstr(Model_Main::get_clean_name($name), Model_Main::get_clean_name($model_name)) !== false) {
                $urls[] = $url;
                echo '<span style="color: red;">' . $name . '</span> - ' . $url . '<br />';   
            }
        }                                 
        return $urls;
    } 
    
    protected function _get_reviews_urls($url)
    {
        $urls = array();
        $this->reviews_url = $url;
        while ($page_data = $this->_get_page_data()) {
            //usleep(700000);
			sleep(5);
            while ($url = $this->_get_next_review_url($page_data)) {
                $urls[] = $url;    
            }    
        }
        return $urls;
    }     */
    
    protected function _get_page_data()
    {
        if (!$this->_reviews_url) {
            return null;
        }
        
        $data = $this->_get_source_code($this->_reviews_url); 
               
        // пихаем в $this->reviews_url адрес следущей страницы
        $this->_reviews_url = $this->_get_next_page_url($data);
        
        return $data;
    }
    
    protected function _get_next_page_url($data)
    {
        if ($url = $this->_cut_str("<a class='npage' title='Следующая' href='", "'>", $data)) {
            return $this->_otzovik_url . $url;      
        }     
        return null;    
    }
	
	protected function _get_review_data(&$data)
    {
        if ($_data = $this->_cut_str("class='list_rew'>", " style='clear: left;'", $data)) {
            return $_data;    
        }
        return null;
    }
    
    /*protected function _get_next_review_url(&$data)
    {
        if ($url = $this->_cut_str('<h3>', '</h3>', $data)) {
            if ($url = $this->_cut_str('<a href=', '>', $url)) {
                return $this->otzovik_url . $url; 
            }   
        }
        return null;   
    }*/
    
    protected function _get_id($data)
    {
        if ($id = $this->_cut_str('<a href=/review_', '.html>', $data)) {
            return $id;    
        }
        return null;
    }
    
    protected function _get_user_nick($data)
    {
        if ($nick = $this->_cut_str('<a class=\'author\'', '</a', $data)) {
            return trim(strip_tags($nick));   
        }
        return null;
    }
    
    protected function _get_user_link(&$data)
    {
        if ($link = $this->_cut_str('id=author href=', '>', $data)) {
            return $this->_otzovik_url . $link;  
        }
        return null;
    }
    
    protected function _get_date($data)
    {       
        if ($date = trim($this->_cut_str('Дата отзыва:</b>', '</span', $data))) {
			$date = substr($date, 0, 10);
			$parts = explode('-', $date);
			$result = array();
			
			if (count($parts) == 3) {
				$result['d'] = $parts[2];
				$result['m'] = $parts[1];
				$result['y'] = $parts[0];   
			} else {
				return null;
			}
			
			$date = strtotime(implode('-', $result));
			if ($date) {
				return $date;
			}                 
        }
        return null;
    }
    
    protected function _get_rating($data)
    { 
		return substr_count($data, 'rating_star_y');
		/*$count = 0;
        while ($this->_cut_str('class=\'rating_star_y', '</div', $data) !== null) { 
			$count++;
        }
        return $count; */  
    }
    
    protected function _get_pluses(&$data)
    {
        if ($pluses = $this->_cut_str('<p><b>Достоинства:</b>', '</', $data)) {
            return trim($pluses);    
        }
        return null;  
    }
    
    protected function _get_minuses(&$data)
    {
        if ($minuses = $this->_cut_str('<p><b>Недостатки:</b>', '</', $data)) {
            return trim($minuses);    
        }
        return null;  
    }
    
    protected function _get_text($data)
    {
        if ($text = $this->_cut_str('p><p>', '<p><b>', $data)) {
            return trim(strip_tags($text));    
        }
        return null;   
    }
    
    
    protected function _get_usefulness($data)
    {
        $usefulness = array();
        $usefulness['yes'] = 0;
        if ($yes = $this->_cut_str("<b>Отзыв рекомендуют:</b> <b style='color:#63B144;'>", '</b', $data)) {
			$usefulness['yes'] = (int)strip_tags($yes);          
        }
        $usefulness['no'] = 0;
        return $usefulness;
    }
}